package com.example.fbztest_vue.controller;

import com.example.fbztest_vue.dao.FindIdAndUsernameByUsernameAndPasswordResult;
import com.example.fbztest_vue.dao.UserMapper;
import com.example.fbztest_vue.entity.TestData;
import com.example.fbztest_vue.entity.User;
import com.example.fbztest_vue.utils.RedisUtil;
import com.example.fbztest_vue.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @PostMapping("login")
    public HashMap<String,Object> login(@RequestBody User user){
        HashMap<String,Object> map=new HashMap<>();
        log.info("获取的loginInfo:{}",user);
        FindIdAndUsernameByUsernameAndPasswordResult result=userMapper.findIdAndUsernameByUsernameAndPassword(user);
        if (result==null){
            log.info("no such user");
            map.put("result","NO_SUCH_USER");
        }else{
            log.info("log in success");
            map.put("result","LOGIN_SUCCESS");
            String token=tokenUtils.getToken(result.getId(),result.getUsername());
            redisUtil.set(result.getId(),token);
            map.put("userInfo",result);
            map.put("token",token);

        }
        log.info("最终返回值为:{}",map);
        return map;
    }


    @Autowired
    TokenUtils tokenUtils;

    @Autowired
    RedisUtil redisUtil;

    @PostMapping("form")
    public void testForm(@RequestParam("strs")List<String> strs){
        log.info("获取的strs:{}",strs);
        for (String s:strs){
            log.info("当前的字符串为:{}",s);
        }
    }

    @PostMapping("form2")
    public void testForm2(@RequestBody TestData testData){
        List<String> strs=testData.getStrs();
        log.info("获取的strs:{}",strs);
        for (String s:strs){
            log.info("当前的字符串为:{}",s);
        }
    }


}
